
#[derive(Debug)]
enum Request {
    REQUESTA,
    REQUESTB,
}

trait Handler<'a> {
    fn set_next(&mut self, next: &'a dyn Handler<'a>) -> &mut dyn Handler<'a>;
    fn handler(&self, request: Request);
}

struct Ahandler<'a> {
    name: String,
    next: Option<&'a dyn Handler<'a>>,
}

impl<'a> Ahandler<'a> {
    fn new(name: String) -> Ahandler<'a> {
        Ahandler {
            name,
            next: None,
        }
    }
}

impl<'a> Handler<'a> for Ahandler<'a> {
    fn set_next(&mut self, next: &'a dyn Handler<'a>) -> &mut dyn Handler<'a> {
        self.next = Some(next);
        self
    }

    fn handler(&self, request: Request) {
        if let Request::REQUESTA = request {
            println!("handler-{} handler request-{:?}", self.name, request);
        }

        if let Some(next) = self.next {
            next.handler(request);
        }
    }
}

struct Bhandler<'a> {
    name: String,
    next: Option<&'a dyn Handler<'a>>,
}

impl<'a> Bhandler<'a> {
    fn new(name: String) -> Bhandler<'a> {
        Bhandler {
            name,
            next: None,
        }
    }
}

impl<'a> Handler<'a> for Bhandler<'a> {
    fn set_next(&mut self, next: &'a dyn Handler<'a>) -> &mut dyn Handler<'a> {
        self.next = Some(next);
        self
    }

    fn handler(&self, request: Request) {
        if let Request::REQUESTB = request {
            println!("handler-{} handler request-{:?}", self.name, request);
        }

        if let Some(next) = self.next {
            next.handler(request);
        }
    }
}

fn main() {
    let mut handler = Ahandler::new("ahandler".to_string());
    let bhandler = Bhandler::new("bhandler".to_string());
    handler.set_next(&bhandler);
    
    let request = Request::REQUESTA;
    handler.handler(request);

    println!("----------------------我是分割线----------------------");

    let request = Request::REQUESTB;
    handler.handler(request);
}